from django.core.paginator import Paginator
from rest_framework.pagination import PageNumberPagination

from utils.addons.responses import PageResponse


class PaginatedResponseMixin:

    def get_paginated_response(self, data):
        msg = 'success'
        request = getattr(self, 'request')
        page = int(getattr(self, 'get_page_number')(request, Paginator)) or 1
        count = getattr(self, 'page').paginator.count
        if not data:
            msg = "暂无数据"
            data = []

        return PageResponse(data, msg, page, count=count)


class CustomPageNumberPagination(PaginatedResponseMixin, PageNumberPagination):
    invalid_page_message = '{message}'
    page_size_query_param = 'page_size'
